import time
from sanic import Sanic
from Config.Config import DBConfig
from can import Can
app = Sanic.get_app("myapp")

'''
{'status': 200, 'msg': '操作成功', 'responseTime': 1695694654067, 'page': None, 'object': '此人是持证残疾人！'}
'''

class CHECKER:
    async def checkDonor(self, num):
        db = DBConfig.db
        cursor = db.cursor()
        if num == 0 or num == None:
            print('每次查询次数不能为0')
            return  "error次数不能为0";
        #排除永拒的 未轮询的记录
        sql = f'select * from  {app.config.STATION}  where is_checked  = 0 and donor_status != "YJ"  limit {num}'
        cursor.execute(sql)
        donors = cursor.fetchall()
        countNUm = 0
        for donor in donors:
            countNUm += 1
            codename = time.strftime('%m%d%H%M%S', time.localtime())
            print(codename + '---' + donor['donor_name'] + '---' + str(countNUm) + '/' + str(len(donors)))
            cancheck = Can()
            code = cancheck.get_code(codename)
            if code == 0 or len(code) != 4:
                print('code error:%s',code)
            else:
                time.sleep(3)
                resp = cancheck.check(code, donor['donor_name'], donor['id_card'])
                #{'status': 200, 'msg': '操作成功', 'responseTime': 1695694654067, 'page': None, 'object': '此人是持证残疾人！'}
                if len(resp) > 0:
                    updateSql = 'update '+app.config.STATION+' set is_checked = 1,error_code= %s,error_message = %s, msg= %s,`status` =%s  ' \
                                'WHERE id_card = %s '
                    cursor.execute(updateSql,
                                   (resp['status'], resp['object'], resp['msg'], resp['responseTime'], donor['id_card']))
                    db.commit()
        #关闭数据库
        return 'sucess'
    async def checkDonorAgain(self, num):
        db = DBConfig.db
        cursor = db.cursor()
        if num == 0 or num == None:
            print('每次查询次数不能为0')
        #排除拒用的; 验证码失败的
        sql = f'select * from  {app.config.STATION} where is_checked  = 1 and error_code = "7010" and donor_status != "YJ"  limit {num} '
        cursor.execute(sql)
        donors = cursor.fetchall()
        print('查询到%s',len(donors))
        countNUm = 0
        for donor in donors:
            countNUm += 1
            codename = time.strftime('%m%d%H%M%S', time.localtime())
            print(codename + '---' + donor['donor_name'] + '---' + str(countNUm) + '/' + str(len(donors)))
            cancheck = Can()
            code = cancheck.get_code(codename)
            if code == 0 or len(code) != 4:
                print('code error:%s',code)
            else:
                time.sleep(2)
                resp = cancheck.check(code, donor['donor_name'], donor['id_card'])
                if resp == 0:
                    print('网络错误')
                else:
                    if resp['status'] != 200:
                        print('check server  != 200 ')
                        print(resp)
                        time.sleep(3)
                        if resp['object'] != '验证码错误！':
                            updateSql = 'update  ' + app.config.STATION + ' set is_checked = 1,error_code= %s,error_message = %s, msg= %s,`status` =%s  ' \
                                                                          'WHERE id_card = %s '
                            cursor.execute(updateSql,
                                           ('7011', resp['object'], resp['msg'], resp['responseTime'],
                                            donor['id_card']))
                            db.commit()
                    else:
                        updateSql = 'update  '+app.config.STATION+' set is_checked = 1,error_code= %s,error_message = %s, msg= %s,`status` =%s  ' \
                                    'WHERE id_card = %s '
                        cursor.execute(updateSql,
                                       (resp['status'], resp['object'], resp['msg'], resp['responseTime'], donor['id_card']))
                        db.commit()
        #关闭数据库
        return 'sucess'
    #系统繁忙
    async def checkDonorSysError(self, num):
        db = DBConfig.db
        cursor = db.cursor()
        if num == 0 or num == None:
            print('每次查询次数不能为0')
        #排除拒用的 验证码失败的
        sql = f'select * from  {app.config.STATION} where is_checked  = 1 and error_code = "7011" and donor_status != "YJ" and msg like("%系统繁忙%") limit {num} '
        cursor.execute(sql)
        donors = cursor.fetchall()
        print('查询到%s',len(donors))
        countNUm = 0
        for donor in donors:
            countNUm += 1
            codename = time.strftime('%m%d%H%M%S', time.localtime())
            print(codename + '---' + donor['donor_name'] + '---' + str(countNUm) + '/' + str(len(donors)))
            cancheck = Can()
            code = cancheck.get_code(codename)
            if code == 0 or len(code) != 4:
                print('code error:',code)
            else:
                time.sleep(2)
                resp = cancheck.check(code, donor['donor_name'], donor['id_card'])
                if resp == 0:
                    print('网络错误')
                else:
                    if resp['status'] != 200:
                        print('check server  != 200 ')
                        print(resp)
                        time.sleep(3)
                        if resp['object'] != '验证码错误！':
                            updateSql = 'update  ' + app.config.STATION + ' set is_checked = 1,error_code= %s,error_message = %s, msg= %s,`status` =%s  ' \
                                                                          'WHERE id_card = %s '
                            cursor.execute(updateSql,
                                           ('7011', resp['object'], resp['msg'], resp['responseTime'],
                                            donor['id_card']))
                            db.commit()
                    else:
                        updateSql = 'update  '+app.config.STATION+' set is_checked = 1,error_code= %s,error_message = %s, msg= %s,`status` =%s  ' \
                                    'WHERE id_card = %s '
                        cursor.execute(updateSql,
                                       (resp['status'], resp['object'], resp['msg'], resp['responseTime'], donor['id_card']))
                        db.commit()
        #关闭数据库
        return 'sucess'
